class Item < ActiveRecord::Base
  belongs_to :course, :counter_cache => true
  has_many :sentences
  #
  #   get appoint user not study complete words
  #
  #   @param size  取出的最大数
  #
  def self.get_incomplete_items_words_from_item(course_id,user_id,size)
    sql = "SELECT *
            FROM  items
            WHERE items.course_id =? AND items.id
            NOT IN (
            SELECT  user_items.item_id
            FROM  user_items
            WHERE  user_items.user_id = ?
            AND user_items.course_id = ?)  LIMIT ? "
    Item.find_by_sql [sql,course_id,user_id,course_id,size]
  end

  #
  #   get appointed user study appointed course's words
  #
  def self.get_study_items_words(course_id,user_id)
    study_arr = Array.new
    once_size = 10
    user_items = UserItem.get_incomplete_items(course_id,user_id,once_size)
    count = user_items.size
    user_items.each do |ui|
      study_arr << ui.item
    end
    unless count == once_size
      item_rows = once_size - count
      item_rows = 0 if item_rows < 0
      items_arr = get_incomplete_items_words_from_item(course_id,user_id,item_rows)
      unless items_arr.blank?
        study_arr = study_arr + items_arr
      end
    end
    study_arr
  end
  #
  #   get not login user study items
  #
  #
  def self.get_study_items_words_not_user(course_id,size = 10)
    Item.find :all, :conditions => ["course_id = ? ", course_id], :limit => size
  end

  def self.per_page
    10
  end
  
end
